﻿using System;
using System.Linq;
using WebExam.DataEntities;
using WebExam.DataProvider;

namespace WebExam.SQLDataProvider
{
    public class ZGRepository : BaseRepository<ZG>, IZGRepository
    {

        /// <summary>
        /// 根据职工名获取用户
        /// </summary>
        /// <param name="name">职工名</param>
        public ZG Get(string name)
        {
            using (ThisDb)
            {
                return ThisTable.FirstOrDefault(p => p.ZGName== name);
            }
        }

        /// <summary>
        /// 根据职工Id获取用户 
        /// </summary>
        /// <param name="zgId">职工Id</param>
        public ZG Get(int zgId)
        {
            using (ThisDb)
            {
                return ThisTable.FirstOrDefault(p => p.ZGId == zgId);
            }
        }

        /// <summary>
        /// 获取职工信息
        /// </summary>
        /// <param name="start">开始页码</param>
        /// <param name="takeSize">获取条数</param>
        public IQueryable<ZG> GetAll(int start, int takeSize)
        {
            using (ThisDb)
            {
                return ThisTable.OrderBy(p => p.ZGName).Skip(start).Take(takeSize);
            }
        }

        /// <summary>
        /// 添加新职工
        /// </summary>
        /// <param name="zg">职工对象</param>
        public ZG Add(ZG zg)
        {
            using (ThisDb)
            {
                try
                {
                    ThisTable.InsertOnSubmit(zg);
                    ThisDb.SubmitChanges();
                    return zg;
                }
                catch
                {
                    return zg;
                }
            }
        }

        /// <summary>
        /// 更新职工信息
        /// </summary>
        /// <param name="zgId">职工Id</param>
        /// <param name="update">包含更新操作的方法</param>
        public ZG Update(int zgId, Action<ZG> update)
        {
            using (ThisDb)
            {
                var obj = ThisTable.FirstOrDefault(p => p.ZGId == zgId);

                if (obj == null)
                {
                    return null;
                }

                update(obj);

                try
                {
                    ThisDb.SubmitChanges();
                    return obj;
                }
                catch
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 删除职工信息
        /// </summary>
        /// <param name="zgId">职工Id</param>
        public bool Delete(int zgId)
        {
            using (ThisDb)
            {
                var obj = ThisTable.FirstOrDefault(p => p.ZGId == zgId);

                if (obj == null)
                {
                    return true;
                }

                try
                {
                    ThisTable.DeleteOnSubmit(obj);
                    ThisDb.SubmitChanges();
                    return true;
                }
                catch
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 是否存在同名职工
        /// </summary>
        /// <param name="name">职工姓名</param>
        /// <returns></returns>
        public bool IsExists(string name)
        {
            using (ThisDb)
            {
                return ThisTable.Count(p => string.Equals(name, p.ZGName, StringComparison.OrdinalIgnoreCase)) > 0;
            }
        }

        /// <summary>
        /// 统计职工人数
        /// </summary>
        public int Count()
        {
            using (ThisDb)
            {
                return ThisTable.Count();
            }
        }

    }
}